/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.pucp.cpnc.mm.renderer;

/**
 *
 * @author Usuario
 */
public class CameraPanController {
    protected static int cameraPanX= 0;
    protected static int cameraPanY = 0;
    private static int cameraLimitX = 10000;
    private static int cameraLimitY = 10000;
    private static boolean camUp = false;
    private static boolean camDown = false;
    private static boolean camRight = false;
    private static boolean camLeft = false;
    private static final int cameraPanRate = 10;
    
    public enum Direction{
        UP(0),DOWN(1),LEFT(2),RIGHT(3);
        private int camVar;
        
        private Direction(int camVar){
            this.camVar = camVar;
        }
        
        private void setCamVar(boolean value){
            switch(camVar){
                case 0: camUp = value;
                    break;
                case 1: camDown = value;
                    break;
                case 2: camLeft = value;
                    break;
                case 3: camRight = value;
                    break;
            }
        }
    }
    
    
    public static void updateCamera(){
        if(camUp == true){
            if(correctBounds(cameraPanX, cameraPanY + cameraPanRate))
                cameraPanY+=cameraPanRate;
        }
        if(camDown == true){
            if(correctBounds(cameraPanX, cameraPanY - cameraPanRate))
                cameraPanY-=cameraPanRate;
        }
        if(camRight == true){
            if(correctBounds(cameraPanX - cameraPanRate, cameraPanY))
                cameraPanX -=cameraPanRate;
        }
        if(camLeft == true){
            if(correctBounds(cameraPanX + cameraPanRate, cameraPanY))
                cameraPanX+=cameraPanRate;
        }
    }
    
    public static void panOn(Direction dir){
        dir.setCamVar(true);
    }
    
    public static void panOff(Direction dir){
        dir.setCamVar(false);
    }
    
    public static int getCameraPanX(){
        return cameraPanX;
    }
    
    public static int getCameraPanY(){
        return cameraPanY;
    }
    
    public static void setCameraLimits(int width, int height, int tileWidth, int tileHeight,int panelWidth, int panelHeight ){
        cameraLimitX = (width/2 + height/2)*tileWidth - panelWidth;
        cameraLimitY = (int)((width + height)*(double)tileHeight/2) - panelHeight + 100;
        cameraPanX = -cameraLimitX/2;
        cameraPanY = (-cameraLimitY + tileHeight/2)/2;
        Renderer.offsetY = (int)Math.round(((double)width-1)*(double)tileHeight/2);
        Renderer.offsetX = 0;
    }
    
    private static boolean correctBounds(int panX, int panY){
        return !(panX<-cameraLimitX || panY< -cameraLimitY || panX>0 || panY>Renderer.Altura/2);
    }
    
    public static void minimapPanEvent(int posX, int posY){
        int newPanX = -posX;
        int newPanY = -posY;
        if(correctBounds(newPanX, newPanY)){
            cameraPanX = newPanX;
            cameraPanY = newPanY;
        }else{
            if(newPanX<-cameraLimitX) cameraPanX = -cameraLimitX;
            if(newPanX>0) cameraPanX = 0;
            if(newPanY>Renderer.Altura/2) cameraPanY=0;
            if(cameraPanY< -cameraLimitY) cameraPanY = -cameraLimitX;
                
            if(correctBounds(newPanX,-cameraLimitY+1)){
                cameraPanX = newPanX;
            }
            if(correctBounds(-cameraLimitX+1,newPanY)){
                cameraPanY = newPanY;
            }
        }
    }
}
